package project.youkumenu;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import project.youkumenu.customview.CustomTitleView;
import project.youkumenu.utils.LogUtil;
import project.youkumenu.utils.UIHandler;

public class Main2Activity extends AppCompatActivity {

    public static final int MESSAGE_FLAG = 0;
    private  CustomTitleView customView;

    private UIHandler<Main2Activity> mHandler = new UIHandler<Main2Activity>(this) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MESSAGE_FLAG) {
                customView.setTitleText(customView.randomText());
                customView.postInvalidate();

                mHandler.sendEmptyMessageDelayed(MESSAGE_FLAG, 1500);

                LogUtil.i("视图：" + customView.getTitleText());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init();
    }

    private void init() {
        customView = (CustomTitleView) findViewById(R.id.custom_view);
        mHandler.sendEmptyMessage(MESSAGE_FLAG);
    }

    @Override
    protected void onDestroy() {
        if (null != mHandler) {
            mHandler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //亮屏时，恢复消息
        if (!mHandler.hasMessages(MESSAGE_FLAG)) {
            mHandler.sendEmptyMessage(MESSAGE_FLAG);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //熄屏时,删除消息
        if (mHandler.hasMessages(MESSAGE_FLAG)) {
            mHandler.removeMessages(MESSAGE_FLAG);
        }
    }
}
